package com.unme.controller;

import com.alibaba.fastjson.JSONObject;
import com.unme.model.Label;
import com.unme.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/apk")
public class LabelController {

    @Autowired
    private LabelService labelService;

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/addLabel", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> addLabel(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Label label=new Label();
        Map<String,Object> map=new HashMap<>();
        try{
            label.setName(jsonParam.getString("name"));
            label.setuKey(jsonParam.getString("uKey"));
            labelService.addLabel(label);
            map.put("message","数据添加成功！");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","数据添加失败！");
            return map;

        }
    }


    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/findLabel", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> findLabel(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Label label=new Label();
        Map<String,Object> map=new HashMap<>();

        try{
            map.put("data",labelService.findAllLabel(label));
            map.put("message","数据查询成功！");
            return map;
        }catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据查询失败！");
            return map;
        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/deleteLabel", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> deleteLabel(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Label label=new Label();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{

            labelService.deleteLabel(label);
            map.put("message","数据删除成功！");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","数据删除失败！");
            return map;

        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/updateLabel", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> updateLabel(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Label label=new Label();
        Map<String, Object> map = new HashMap<>();
        String oa = request.getParameter("oa");
        try {
            label.setName(jsonParam.getString("name"));
            label.setuKey(jsonParam.getString("uKey"));
            labelService.updateLabel(label);
            map.put("message", "数据更新成功！");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据更新失败！");
            return map;

        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/getLabelById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> getLabelById(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        Label label=new Label();
        Map<String, Object> map = new HashMap<>();
        String oa = request.getParameter("oa");
        try {
            label.setName(jsonParam.getString("name"));
            label.setuKey(jsonParam.getString("uKey"));
            labelService.getLabelById(label);
            map.put("message", "数据获取成功！");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据获取失败！");
            return map;

        }
    }
}
